package com.efast.cafe.portal.service.api.impl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.efast.cafe.portal.service.api.IPortalApiExtfieldService;

import java.util.List;
import java.util.UUID;

import com.efast.cafe.framework.constant.CommonConstant;
import com.efast.cafe.framework.redis.ObjectRedisTemplate;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.cafe.portal.bean.api.PortalApiExtfieldBean;
import com.efast.cafe.portal.bean.api.PortalApiExtfieldDto;
import com.efast.cafe.portal.dao.api.PortalApiExtfieldDao;
import com.efast.cafe.portal.entity.api.PortalApiExtfield;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;


/**
 * <p>Service接口实现类</p>
 * <p>Table: portal_api_extfield - </p>
 *
 * @since 2017-09-12 03:54:20
 */
@Service
@Transactional
public class PortalApiExtfieldServiceImpl implements IPortalApiExtfieldService{

	private PortalApiExtfieldDao portalApiExtfieldDao;
	
	@Autowired
	private ObjectRedisTemplate objectRedisTemplate;
	
	@Autowired
	public void setPortalApiExtfieldDao(PortalApiExtfieldDao portalApiExtfieldDao) {
		this.portalApiExtfieldDao = portalApiExtfieldDao;
	}
	
	@Autowired
	private StringRedisTemplate stringRedisTemplate;

	/**
	 * 新增扩展字段
	 * {@inheritDoc} 
	 * overridden:
	 * @Date        :      2017年9月13日
	 * @see com.efast.cafe.portal.remote.api.PortalApiExtfieldService#insertPortalApiExtfield(com.efast.cafe.portal.bean.api.PortalApiExtfieldBean)
	*
	 */
	@Override
	public void insertPortalApiExtfield(PortalApiExtfieldBean apiExtfieldBean) {
		if(!apiExtfieldBean.getExtfieldIds().isEmpty()){
			portalApiExtfieldDao.delPortalApiExtfieldByIds(apiExtfieldBean.getExtfieldIds());
			stringRedisTemplate.opsForValue().set(CommonConstant.SESSION_ENTITY_VER, UUID.randomUUID().toString());

		}
		// TODO Auto-generated method stub
		if(!apiExtfieldBean.getExtfieldList().isEmpty()){
			portalApiExtfieldDao.insertPortalApiExtfield(apiExtfieldBean.getExtfieldList(),apiExtfieldBean.getObjId(),apiExtfieldBean.getEntityName(),SpringWebUtils.getRequestUser(),SpringWebUtils.getRequestCompany());
		}
	}

	/**
	 * 查询扩展字段详情    只能查到自己企业的字段
	 * {@inheritDoc} 
	 * overridden:
	 * @Date        :      2017年9月14日
	 * @see com.efast.cafe.portal.remote.api.PortalApiExtfieldService#queryObjByObjId(java.lang.Integer)
	*
	 */
	@Override
	public PortalApiExtfieldDto queryExtfieldByObjId(Integer objId) {
		// TODO Auto-generated method stub
		return portalApiExtfieldDao.queryExtfieldByObjId(objId,SpringWebUtils.getRequestCompany());
	}

	/**
	 * 删除扩展字段
	 * {@inheritDoc} 
	 * overridden:
	 * @Date        :      2017年9月14日
	 * @see com.efast.cafe.portal.remote.api.PortalApiExtfieldService#delPortalApiExtfield(java.lang.Integer)
	*
	 */
	@Override
	public void delPortalApiExtfield(Integer id) {
		// TODO Auto-generated method stub
		portalApiExtfieldDao.delPortalApiExtfield(id);
	}

	/**
	 * 修改扩展字段   
	 * {@inheritDoc} 
	 * overridden:
	 * @Date        :      2017年9月14日
	 * @see com.efast.cafe.portal.remote.api.PortalApiExtfieldService#updateApiExtfield(com.efast.cafe.portal.bean.api.PortalApiExtfieldBean)
	*
	 */
	@Override
	public void updateApiExtfield(PortalApiExtfieldBean apiExtfieldBean) {
		// TODO Auto-generated method stub
		if(!apiExtfieldBean.getExtfieldIds().isEmpty()){
			portalApiExtfieldDao.delPortalApiExtfieldByIds(apiExtfieldBean.getExtfieldIds());
		}
		if(!apiExtfieldBean.getExtfieldList().isEmpty()){
			portalApiExtfieldDao.insertPortalApiExtfield(apiExtfieldBean.getExtfieldList(),apiExtfieldBean.getObjId(),apiExtfieldBean.getEntityName(),SpringWebUtils.getRequestUser(),SpringWebUtils.getRequestCompany());
		}
	}

	/**
	 *  新增扩展字段平台级别
	 */
	@Override
	public void insertPortalApiExtfield_portal(PortalApiExtfieldBean apiExtfieldBean) {
		// TODO Auto-generated method stub
		if(!apiExtfieldBean.getExtfieldIds().isEmpty()){
			portalApiExtfieldDao.delPortalApiExtfieldByIds(apiExtfieldBean.getExtfieldIds());
		}
		// TODO Auto-generated method stub
		if(!apiExtfieldBean.getExtfieldList().isEmpty()){
			portalApiExtfieldDao.insertPortalApiExtfield_portal(apiExtfieldBean.getExtfieldList(),apiExtfieldBean.getObjId(),apiExtfieldBean.getEntityName(),SpringWebUtils.getRequestUser());
		}
	}

	/**
	 * 查询扩展字段详情 --------- 平台级别,能查到所有的字段
	 */
	@Override
	public PortalApiExtfieldDto queryExtfieldByObjId_portal(Integer objId) {
		// TODO Auto-generated method stub
		return portalApiExtfieldDao.queryExtfieldByObjId(objId,null);
	}
	
	
}